package com.zcfeng.ischool;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;


import Xlist.XListView;
import Xlist.XListView.IXListViewListener;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.PersistableBundle;
import android.support.v4.app.ActivityCompat;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.ExpandableListView.OnChildClickListener;
import bean.Articlebean;
import db.dao.ArticleDao;
import android.widget.ExpandableListView;
import utils.ToastUtils;
import utils.TreeViewAdapter;

public class SchoolWayActivity extends Activity implements IXListViewListener
{
	XListView list=null;
	private List<TreeViewAdapter.TreeNode> treeNode;
	private Handler mHandler;
	private TreeViewAdapter adapter;
	public String[] groups = { "生活娱乐","院系介绍", "学习天地"  };
	private ArrayList<ArrayList<Articlebean>> data=new ArrayList<ArrayList<Articlebean>>();
	private void initview() {

		list=(XListView)findViewById(R.id.xListView);

	}

	private void initData() {
		adapter = new TreeViewAdapter(getApplicationContext(), 38);
		treeNode = adapter.getTreeNode();
		for (int i = 0; i < groups.length; i++) {
			TreeViewAdapter.TreeNode node = new TreeViewAdapter.TreeNode();
			node.parent = groups[i];
			ArrayList<Articlebean> temp=data.get(i);
			for(int j=0;j<temp.size();j++)
				node.childs.add(temp.get(j));
			treeNode.add(node);
		}
		adapter.updateTreeNode(treeNode);
		list.setAdapter(adapter);
		list.expandGroup(0);
	}
	OnChildClickListener stvClickEvent = new OnChildClickListener() {
		@Override
		public boolean onChildClick(ExpandableListView parent, View v,
									int groupPosition, int childPosition, long id) {
			Intent intent =new Intent(getApplicationContext(),TextActivity.class);
			Bundle bundle=new Bundle();
			bundle.putSerializable("article",data.get(groupPosition).get(childPosition));
			intent.putExtras(bundle) ;
			startActivity(intent);
			return false;
		}
	};
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.activity_schoolway);
		mHandler=new Handler();
		initview();
		getData();
		initData();
		initlistener();
	}
	private void initlistener() {
		list.setOnChildClickListener(stvClickEvent);
		list.setXListViewListener(this);
	}
	private  void getData() {
		data.clear();
		for (int i = 0; i < groups.length; i++) {
			ArrayList<Articlebean> temp=ArticleDao.findBytype(groups[i], getApplicationContext());
			data.add(temp);
		}
	}

	private void onLoad()  {
		list.setRefreshTime("刚刚");
		list.stopRefresh();
		list.stopLoadMore();


	}

	// 刷新
	public void onRefresh() {
		ArticleDao.update(getApplicationContext());
		mHandler.postDelayed(new Runnable() {
			@Override
			public void run() {
				getData();
				initData();
				onLoad();
			}
		}, 2000);
	}
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if(keyCode==KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0){
			finish();
		}
		return false;
	}
	@Override
	public void onLoadMore() {

	}
	public void  back(View v){
		finish();

	}
}
